package it.koutifaris.travianmap.view.playerdetails;

import it.koutifaris.travianmap.context.ContextRetreiver;
import it.koutifaris.travianmap.model.Player;
import it.koutifaris.travianmap.model.Village;
import it.koutifaris.travianmap.view.playerdetails.PlayerDetailsView.VillageItemView;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

class VillagesAdapter extends ArrayAdapter<Village> {

	private final PlayerDetailsView view;

	public VillagesAdapter(Player player, PlayerDetailsView view) {
		super(getCurrentContext(), android.R.layout.simple_list_item_1);

		this.view = view;

		for (Village village : player.getVillages()) {
			this.add(village);
		}
	}

	private static Context getCurrentContext() {
		return ContextRetreiver.contextRetreiver().getCurrentContext().getActivity();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View androidView;

		if (convertView != null) {
			androidView = convertView;
		} else {
			androidView = view.newItemView(parent);
		}

		Village village = this.getItem(position);

		VillageItemView villageItemView = view.itemViewFor(androidView);
		villageItemView.setVillageName(village.getName());
		villageItemView.setPopulation(village.getPopulation());

		return androidView;
	}

}
